//
//  ZM_LT_TVCell.m
//  YYFMMall
//
//  Created by chenzm on 2019/9/24.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "ZM_LT_TVCell.h"

@interface ZM_LT_TVCell()<UITextFieldDelegate>

@end

@implementation ZM_LT_TVCell

#pragma mark - 赋值
-(void)setShowType:(NSInteger)showType{
    _showType = showType;
    
    switch (showType) {
        case 0:{
            self.bgV.hidden = YES;
            self.lbl.hidden = YES;
            self.lineV.hidden = NO;
            self.textF.hidden = NO;
            
            [_textF mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.right.mas_equalTo(-15);
                make.centerY.mas_equalTo(self);
                make.height.mas_equalTo(30);
            }];
            [_lineV mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self);
                make.left.mas_equalTo(15);
                make.right.mas_equalTo(-15);
                make.height.mas_equalTo(1);
            }];
        }break;
            
        default:{
            self.bgV.hidden = YES;
            self.lbl.hidden = NO;
            self.lineV.hidden = YES;
            self.textF.hidden = NO;
            [_textF mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.right.mas_equalTo(-15);
                make.centerY.mas_equalTo(self);
                make.height.mas_equalTo(30);
            }];
        }break;
    }
}


#pragma mark - Methods
//获取文本框中的值
- (NSString *)getCellValue{
    NSString *str = [self.textF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    return str;
}


#pragma mark - Intial
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

///基本数据设置
-(void)setUpBaseData{
    
}
///添加控件
-(void)setUpUI{
    [self bgV];
    [self lbl];
    [self textF];
    [self lineV];
}


#pragma mark - 布局
-(void)layoutSubviews{
    [super layoutSubviews];
    
}

#pragma mark - textFieldDelegate
//文本即将开始编译
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
//文本点击返回键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.textF resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([textField.placeholder isEqualToString:@"输入主题"]) {
        if (range.location+string.length > 40) {
            return NO;
        }
    }
    
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    !self.txtDidEditingBlock?:self.txtDidEditingBlock(textField,toBeString);
    return [ZMConfigTools judgeTextF:textField range:range replaceString:string placeholder:self.textF.placeholder];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    !self.txtDidEndBlock?:self.txtDidEndBlock(textField,textField.text);
    return YES;
}


#pragma mark - lazyLoad

-(UIView *)bgV{
    if (!_bgV) {
        _bgV = [UIView new];
        _bgV.frame = CGRectZero;
        _bgV.backgroundColor = kBgColor;
        _bgV.hidden = YES;
        [self addSubview:_bgV];
    }
    return _bgV;
}

-(UILabel *)lbl{
    if (!_lbl) {
        _lbl = [UILabel new];
        _lbl.frame = CGRectZero;
        _lbl.tintColor = kBlackColor;
        _lbl.font = kMediumFont(15);
        _lbl.hidden = YES;
        _lbl.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_lbl];
    }
    return _lbl;
}

-(UITextField *)textF{
    if (!_textF) {
        _textF = [UITextField new];
        _textF.delegate = self;
        _textF.font = kMediumFont(14);
        _textF.tintColor = kDarkGrayColor;
        _textF.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self addSubview:_textF];
        [_textF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self);
            make.height.mas_equalTo(30);
        }];
    }
    return _textF;
}

-(UIView *)lineV{
    if (!_lineV) {
        _lineV = [UIView new];
        _lineV.hidden = YES;
        _lineV.backgroundColor = kHexColor(0xEEEEEE);
        [self addSubview:_lineV];
        [_lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(1);
        }];
    }
    return _lineV;
}


@end

